package com.openbravo.data.loader;

import com.openbravo.basic.BasicException;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.sql.Timestamp;
import java.util.Date;
import java.util.logging.Logger;

public class PreparedSentence extends JDBCSentence
{
  private static Logger logger = Logger.getLogger("com.openbravo.data.loader.PreparedSentence");
  private String m_sentence;
  protected SerializerWrite m_SerWrite = null;
  protected SerializerRead m_SerRead = null;
  private PreparedStatement m_Stmt_prepareStatement;

  public PreparedSentence(Session paramSession, String paramString, SerializerWrite paramSerializerWrite, SerializerRead paramSerializerRead)
  {
    super(paramSession);
    this.m_sentence = paramString;
    this.m_SerWrite = paramSerializerWrite;
    this.m_SerRead = paramSerializerRead;
    this.m_Stmt_prepareStatement = null;
  }

  public PreparedSentence(Session paramSession, String paramString, SerializerWrite paramSerializerWrite)
  {
    this(paramSession, paramString, paramSerializerWrite, null);
  }

  public PreparedSentence(Session paramSession, String paramString)
  {
    this(paramSession, paramString, null, null);
  }

  @SuppressWarnings("unchecked")
public DataResultSet openExec(Object paramObject)
    throws BasicException
  {
	  
//	  if(true)
//		  throw new RuntimeException("aa");
    closeExec();
    try
    {
      logger.info("Executing prepared SQL: " + this.m_sentence);
      this.m_Stmt_prepareStatement = this.m_s.getConnection().prepareStatement(this.m_sentence);
      if (this.m_SerWrite != null)
        this.m_SerWrite.writeValues(new PreparedSentencePars(this.m_Stmt_prepareStatement), paramObject);
      if (this.m_Stmt_prepareStatement.execute()) {
		ResultSet resultSet = this.m_Stmt_prepareStatement.getResultSet();
		//org.apache.derby.impl.jdbc.EmbedResultSet40@1c0dc84    com.openbravo.data.loader.SerializerReadString@6c5624
		return new JDBCSentence.JDBCDataResultSet(resultSet, this.m_SerRead);
	}
      int i = this.m_Stmt_prepareStatement.getUpdateCount();
      if (i < 0)
        return null;
      return new SentenceUpdateResultSet(i);
    }
    catch (SQLException localSQLException)
    {    throw new BasicException(localSQLException);
    }

  }

  public final DataResultSet moreResults()
    throws BasicException
  {
    try
    {
      if (this.m_Stmt_prepareStatement.getMoreResults())
        return new JDBCSentence.JDBCDataResultSet(this.m_Stmt_prepareStatement.getResultSet(), this.m_SerRead);
      int i = this.m_Stmt_prepareStatement.getUpdateCount();
      if (i < 0)
        return null;
      return new SentenceUpdateResultSet(i);
    }
    catch (SQLException localSQLException)
    {throw new BasicException(localSQLException);
    }
    
  }

  public final void closeExec()
    throws BasicException
  {
    if (this.m_Stmt_prepareStatement != null)
      try
      {
        this.m_Stmt_prepareStatement.close();
      }
      catch (SQLException localSQLException)
      {
        throw new BasicException(localSQLException);
      }
      finally
      {
        this.m_Stmt_prepareStatement = null;
      }
  }

  private static final class PreparedSentencePars
    implements DataWrite
  {
    private PreparedStatement m_ps;

    PreparedSentencePars(PreparedStatement paramPreparedStatement)
    {
      this.m_ps = paramPreparedStatement;
    }

    public void setInt(int paramInt, Integer paramInteger)
      throws BasicException
    {
      try
      {
        this.m_ps.setObject(paramInt, paramInteger, 4);
      }
      catch (SQLException localSQLException)
      {
        throw new BasicException(localSQLException);
      }
    }

    public void setString(int paramInt, String paramString)
      throws BasicException
    {
      try
      {
        this.m_ps.setString(paramInt, paramString);
      }
      catch (SQLException localSQLException)
      {
        throw new BasicException(localSQLException);
      }
    }

    public void setDouble(int paramInt, Double paramDouble)
      throws BasicException
    {
      try
      {
        this.m_ps.setObject(paramInt, paramDouble, 8);
      }
      catch (SQLException localSQLException)
      {
        throw new BasicException(localSQLException);
      }
    }

    public void setBoolean(int paramInt, Boolean paramBoolean)
      throws BasicException
    {
      try
      {
        if (paramBoolean == null)
          this.m_ps.setObject(paramInt, null);
        else
          this.m_ps.setBoolean(paramInt, paramBoolean.booleanValue());
      }
      catch (SQLException localSQLException)
      {
        throw new BasicException(localSQLException);
      }
    }

    public void setTimestamp(int paramInt, Date paramDate)
      throws BasicException
    {
      try
      {
        this.m_ps.setObject(paramInt, paramDate == null ? null : new Timestamp(paramDate.getTime()), 93);
      }
      catch (SQLException localSQLException)
      {
        throw new BasicException(localSQLException);
      }
    }

    public void setBytes(int paramInt, byte[] paramArrayOfByte)
      throws BasicException
    {
      try
      {
        this.m_ps.setBytes(paramInt, paramArrayOfByte);
      }
      catch (SQLException localSQLException)
      {
        throw new BasicException(localSQLException);
      }
    }

    public void setObject(int paramInt, Object paramObject)
      throws BasicException
    {
      try
      {
        this.m_ps.setObject(paramInt, paramObject);
      }
      catch (SQLException localSQLException)
      {
        throw new BasicException(localSQLException);
      }
    }
  }
}

/* Location:           D:\PowerFulPOS-2.30.2\openbravopos.jar
 * Qualified Name:     com.openbravo.data.loader.PreparedSentence
 * JD-Core Version:    0.6.0
 */